﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class CatalogTree : Form
    {
        DataSets.DictionaryTableAdapters.CatalogTableAdapter _adapter;
        DataSets.Dictionary.CatalogDataTable _data;
        public DataSets.DictionaryTableAdapters.CatalogTableAdapter Adapter
        {
            get
            {
                if (_adapter == null)
                    _adapter = new DIProject.DataSets.DictionaryTableAdapters.CatalogTableAdapter();
                return _adapter;
            }
        }

        public CatalogTree()
        {
            InitializeComponent();
        }

        private void CatalogTree_Load(object sender, EventArgs e)
        {
            lock (this)
            {
                this.Cursor = Cursors.WaitCursor;
                InitTree();
                this.Cursor = Cursors.Default;
            }
        }
        private void InitTree()
        {
            
            DataSets.Dictionary.CatalogDataTable catalog = Adapter.GetData(DIProject.Data.Sync.IsAdmin);
            _data = catalog;
            TreeNode tn = catTV.Nodes.Add("Все категории");
            tn.Tag = 0;
            InitTreeRecursive(tn, catalog);
            
        }
        public void ReInitTree()
        {
            catTV.Nodes.Clear();
            InitTree();
        }
        private void InitTreeRecursive(TreeNode tn, DataSets.Dictionary.CatalogDataTable catalog)
        {
            catalog.DefaultView.RowFilter = "ParentId=" + Convert.ToString(tn.Tag);
            foreach (DataRowView drv in catalog.DefaultView)
            {
                DataSets.Dictionary.CatalogRow cr = drv.Row as DataSets.Dictionary.CatalogRow;
                TreeNode t = tn.Nodes.Add(cr.Name + GetMarkUp(cr));
                t.ContextMenuStrip = this.cms;
                t.Tag = cr.Id;
                InitTreeRecursive(t, catalog);
            }
        }
        private string GetMarkUp(DataSets.Dictionary.CatalogRow cr)
        {
            if (cr.IsMarkUpNull())
                return " (Наценка 0)";
            return " (Наценка " + cr.MarkUp.ToString(System.Globalization.CultureInfo.CurrentCulture) + ")";
        }
        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Int32 ci = Convert.ToInt32(catTV.SelectedNode.Tag);
            CatalogDetails cd = new CatalogDetails(Util.ActionType.Update, ci, this._data);
            cd.MdiParent = this.MdiParent;
            cd.Show();
            
            ReInitTree();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            ReInitTree();
        }

        private void catTV_MouseDown(object sender, MouseEventArgs e)
        {
            TreeViewHitTestInfo ht = catTV.HitTest(e.X, e.Y);
            if (ht != null)
                catTV.SelectedNode = ht.Node;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CatalogDetails cd = new CatalogDetails(Util.ActionType.Insert, Int32.MinValue, this._data);
            cd.MdiParent = this.MdiParent;
            cd.Show();
        }
    }
}